package com.tanhua.management.controller;

import com.tanhua.management.service.CommentService;
import com.tanhua.management.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/manage/messages")

public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     *  评论列表翻页
     * @param sortProp 排序字段
     * @param page
     * @param pageSize
     * @param sortOrder scending 升序 descending 降序
     * @param messageID 消息ID
     * @return
     */
    @GetMapping("comments")
    public ResponseEntity<PageResult> queryComment(@RequestParam(value = "sortProp",defaultValue = "created") String sortProp,
                                                   @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                   @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                   @RequestParam(value = "sortOrder",defaultValue = "descending") String sortOrder,
                                                   @RequestParam("messageID") String messageID) {
        try {
            PageResult pageResult = this.commentService.queryComment(sortProp,page,pageSize,sortOrder,messageID);
            if(pageResult.getItems() != null){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}

